package com.czl.myRabbitMq.mqServer.dataCenter;

import com.czl.myRabbitMq.mqServer.core.Binding;
import com.czl.myRabbitMq.mqServer.core.Exchange;
import com.czl.myRabbitMq.mqServer.core.MSGQueue;
import com.czl.myRabbitMq.mqServer.core.Message;
import lombok.extern.slf4j.Slf4j;

import java.util.LinkedList;
import java.util.List;

/**
 * 该类是用来综合文件和交换机操作的类
 * 也是对外提供接口访问的类
 * 后续操作内存和硬盘的时候 就是直接使用这个类了
 * 就不需要去使用DataManager和MessageFileManager了
 */
@Slf4j
public class DiskDataManager {
    private final DataManager dataManager = new DataManager();

    private final MessageFileManager messageFileManager = new MessageFileManager();

    public void init(){

        dataManager.init();
        //目前此处是空方法 为了后续扩展和代码美观
        messageFileManager.init();
    }

    public void insertExchange(Exchange exchange){
        dataManager.insertExchange(exchange);
    }

    public void deleteExchange(String exchangeName){
        dataManager.deleteExchange(exchangeName);
    }

    public List<Exchange> selectAllExchange(){
        return dataManager.selectAllExchange();
    }

    //在这里插入MSGQueued的时候 就需要为每个Queue下面创建stat和data文件
    public void  insertQueue(MSGQueue queue){
        messageFileManager.createQueueDefaultFile(queue.getName());
        dataManager.insertQueue(queue);
    }
    //同理在进行删除操作的时候，也需要将queue文件下的文件给删除
    public void deleteQueue(String queueName){
        messageFileManager.deleteQueueFile(queueName);
        dataManager.deleteQueue(queueName);
    }

    public List<MSGQueue> selectAllQueue(){
        return dataManager.selectAllQueue();
    }

    public void insertBinding(Binding binding){
        dataManager.insertBinding(binding);
    }

    public void deleteBinding(String ExchangeName, String queueName){
        dataManager.deleteBinding(ExchangeName, queueName);
    }
    public List<Binding> selectAllBinding(){
        return dataManager.selectAllBinding();
    }

    public void writeMessage(Message message, MSGQueue queue){

        messageFileManager.writeMessageData(queue, message);
    }
    //这里不止是删除 还要判断完删除后是否满足GC的条件
    public void deleteMessage(Message message, MSGQueue queue){
        messageFileManager.deleteMessageData(queue, message);

        if(messageFileManager.checkGC(queue.getName()))
            messageFileManager.gc(queue);
    }

    public LinkedList<Message> loudAllMessage(String queueName){
        return messageFileManager.loadMessage(queueName);
    }





}
